import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/pay_free.dart';
import 'package:app/src/http/bean/wroks/pay_notice_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/receipt_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///免款审核：免款（未对账）
class UiPayFreeAdapter3 {
  static Widget build(BuildContext context, List<PayFreeDetail>? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).PayFreeInfo2; //免款明细
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(
      context,
      data,
    ));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    List<PayFreeDetail>? data,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (data != null && data.length > 0) {
          var postion = 1;
          data.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(
                children: cells,
                decoration: BoxDecoration(
                    color: AppColors.getColorWithTableRow(postion))));
          });
        }

        // ///合计
        // if (data != null) {
        //   children.add(TableRow(
        //       //最后一行，添加背景色
        //       decoration:
        //           BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
        //       children: _aCombined(context, data!)));
        // }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          // columnWidths: {
          //   0: FixedColumnWidth(50),
          //   1: FixedColumnWidth(150),
          //   6: FixedColumnWidth(180),
          // },
          border: AppStyle.getTableBorder(),
          textDirection: TextDirection.ltr,
          children: children,
        );
        var width = 2800.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    //children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).OrderCode)); //订单编号
    children.add(_addItem(AppStrings.of(context).StockInCode)); //入库单号
    children.add(_addItem(AppStrings.of(context).StatementCode)); //对账单号
    children.add(_addItem(AppStrings.of(context).ProductName2)); //货品名称
    children.add(_addItem(AppStrings.of(context).Width_Height)); //规格型号
    children.add(_addItem(AppStrings.of(context).Num2)); //数量
    children.add(_addItem(AppStrings.of(context).SumArea)); //面积
    children.add(_addItem(AppStrings.of(context).Price)); //单价
    children.add(_addItem(AppStrings.of(context).AfterPrice)); //减免后单价
    children.add(_addItem(AppStrings.of(context).ThisCutMoney)); //减免金额
    children.add(_addItem(AppStrings.of(context).AfterAmount2)); //减免后金额
    children.add(_addItem(AppStrings.of(context).Amount7)); //发货金额
    children.add(_addItem(AppStrings.of(context).SumOtherAmount2)); //特殊加工费
    children.add(_addItem(AppStrings.of(context).AfterOtherAmount2)); //减免后特殊加工费
    children.add(_addItem(AppStrings.of(context).DeliveryDatetime2)); //发货时间
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      PayFreeDetail element, StateSetter setState) {
    List<Widget> children = [];
    //children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.OrderCode)); //订单编号
    children.add(_addItem(element.StockInCode)); //入库单号
    children.add(_addItem(element.StatementCode)); //对账单号
    children.add(_addItem(element.ProductName)); //货品名称
    children.add(_addItem(element.Width_Height)); //规格型号
    children.add(_addItem(element.Num)); //数量
    children.add(_addItem(element.SumArea)); //面积
    children.add(_addItem(element.Price)); //单价
    children.add(_addItem(element.AfterPrice)); //减免后单价
    children.add(_addItem(element.ThisCutMoney)); //减免金额
    children.add(_addItem(element.AfterAmount)); //减免后金额
    children.add(_addItem(element.Amount)); //发货金额
    children.add(_addItem(element.SumOtherAmount)); //特殊加工费
    children.add(_addItem(element.AfterOtherAmount)); //减免后特殊加工费
    children.add(_addItem(element.DeliveryDatetime)); //发货时间
    return children;
  }

  ///合计,fixme 免款审核的合计，服务器已经合计了，在数据集合的最后一个。
  // static List<Widget> _aCombined(
  //     BuildContext context, List<PayFreeDetail> datas) {
  //   List<Widget> children = [];
  //   children.add(_addItem(AppStrings.of(context).ACombined)); //合计
  //   children.add(_addItem("")); //订单编号
  //   children.add(_addItem("")); //对账单号
  //   children.add(_addItem("")); //货品名称
  //   children.add(_addItem("")); //规格型号
  //   double Num = 0;
  //   datas?.forEach((element) {
  //     var str = element.Num;
  //     Num = AcombinedUtils.add(Num, str);
  //   });
  //   children.add(_addItem(AcombinedUtils.getNum(Num))); //fixme 原数量
  //   double CutNum = 0;
  //   datas?.forEach((element) {
  //     var str = element.CutNum;
  //     CutNum = AcombinedUtils.add(CutNum, str);
  //   });
  //   children.add(_addItem(AcombinedUtils.getNum(CutNum))); //fixme 免款数量
  //   double Amount1 = 0;
  //   datas?.forEach((element) {
  //     var str = element.Amount1;
  //     Amount1 = AcombinedUtils.add(Amount1, str);
  //   });
  //   children.add(_addItem(AcombinedUtils.getMoney(Amount1))); //fixme 免款金额
  //   double SumArea = 0;
  //   datas?.forEach((element) {
  //     var str = element.SumArea;
  //     SumArea = AcombinedUtils.add(SumArea, str);
  //   });
  //   children.add(_addItem(AcombinedUtils.getArea(SumArea))); //fixme 面积
  //   children.add(_addItem("")); //单价
  //   double Amount = 0;
  //   datas?.forEach((element) {
  //     var str = element.Amount;
  //     Amount = AcombinedUtils.add(Amount, str);
  //   });
  //   children.add(_addItem(AcombinedUtils.getMoney(Amount))); //fixme 发货金额
  //   children.add(_addItem("")); //架卡号
  //   return children;
  // }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text?.trim() ?? "",
      style: style,
    );
  }
}
